package com.yylc.cscore.config;

import com.yylc.cscore.component.JobBean;
import com.yylc.cscore.component.JobTrigger;
import org.quartz.Scheduler;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.io.ClassPathResource;
import org.springframework.scheduling.quartz.SchedulerFactoryBean;

import javax.sql.DataSource;

/**
 * @author xiangnan
 * date 2018/4/26
 */
@Configuration
public class CoreConfig {

    /**
     * quartz配置
     */
    @Bean
    public SchedulerFactoryBean schedulerFactoryBean(DataSource dataSource) {
        SchedulerFactoryBean schedulerFactoryBean = new SchedulerFactoryBean();

        schedulerFactoryBean.setDataSource(dataSource);
        schedulerFactoryBean.setAutoStartup(true);
        schedulerFactoryBean.setStartupDelay(20);
        schedulerFactoryBean.setOverwriteExistingJobs(true);
        schedulerFactoryBean.setApplicationContextSchedulerContextKey("applicationContextKey");
        schedulerFactoryBean.setConfigLocation(new ClassPathResource("quartz.properties"));

        return schedulerFactoryBean;
    }

    @Bean(name = "internalScheduler")
    public Scheduler scheduler(SchedulerFactoryBean schedulerFactoryBean) {
        return schedulerFactoryBean.getScheduler();
    }

    /**
     * 需要先注入JobTrigger供JobBean使用
     */
    @Bean
    public JobBean jobBean(JobTrigger trigger) {
        return new JobBean(trigger);
    }

}
